package com.bee.shepherd.common.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

public class CommonDateUtil {

    public static final String YYYYMMDD="yyyy-MM-dd";
    public static final String HHMMSS="HH:mm:ss";
    public static final String YYYYMMDD_HHMMSS="yyyy-MM-dd HH:mm:ss";

    public static final DateTimeFormatter formatEndWithSecond = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter formatEndWithMinut = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
    public static final DateTimeFormatter formatEndWithDay = DateTimeFormat.forPattern("yyyy-MM-dd");
    public static final DateTimeFormatter tightFormatEndWithSecond = DateTimeFormat.forPattern("yyyyMMddHHmmss");
    public static final DateTimeFormatter yyFormatEndWithSecond = DateTimeFormat.forPattern("yyMMddHHmmss");
    public static final DateTimeFormatter tightFormatEndWithDay = DateTimeFormat.forPattern("yyyyMMdd");
    public static final DateTimeFormatter tightFormatWithYear = DateTimeFormat.forPattern("yyyy");

    public static String toString(Date date, DateTimeFormatter format) {
        if (date == null) {
            return "";
        }
        return new DateTime(date).toString(format);
    }

    public static String toStringWhole(Date date) {
        return new DateTime(date).toString(formatEndWithSecond);
    }

    public static Date convertDate(String dateStr, DateTimeFormatter format) {
        return format.parseDateTime(dateStr).toDate();
    }
}
